package com.hopes.bizz.common.core.validate.validator;

import cn.hutool.core.util.StrUtil;
import com.hopes.bizz.common.core.annotation.ExcelDictImportValidate;
import com.hopes.boss.admin.api.resolver.DictLoader;
import com.hopes.boss.admin.api.resolver.DictResolver;
import com.hopes.boss.admin.api.resolver.EnumResolver;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

/**
 * @author: ljh
 * @create: 2023/10/24
 */
public class ExcelDictImportValidator implements ConstraintValidator<ExcelDictImportValidate, String> {


    private EnumResolver<?> enumResolver = null;

    @Override
    public void initialize(ExcelDictImportValidate constraintAnnotation) {
        ConstraintValidator.super.initialize(constraintAnnotation);
        enumResolver = DictLoader.getInstance().getEnumResolverByType(constraintAnnotation.enumResolver());
    }

    @Override
    public boolean isValid(String value, ConstraintValidatorContext context) {
        if (StrUtil.isBlank(value)){
            return true;
        }
        String itemValueByLabel = DictResolver.getItemValueByLabel(enumResolver, value);
        return !StrUtil.isBlank(itemValueByLabel);
    }
}
